{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 打印\n",
    "\n",
    "如我们所见，SymPy能够使用Unicode字符漂亮的打印它的输出。下面会对此做一个简单的入门介绍。\n",
    "\n",
    "### 打印者\n",
    "\n",
    "这里有若干种不同的打印者在SymPy。最常见的如下: \n",
    "\n",
    "- str\n",
    "- srepr\n",
    "- ASCII pretty printer\n",
    "- Unicode pretty printer\n",
    "- LaTeX\n",
    "- MathML\n",
    "- Dot\n",
    "\n",
    "不止于此，也有一些打印者可以输出SymPy对象到代码，诸如C, Fortran, JavaScript, Theano以及Python。这些在本教程中未涉及。\n",
    "\n",
    "### 安装Pretty Printing\n",
    "\n",
    "如果您想要的只是最好的打印效果，请使用init_printing（）函数。 这将自动启用您环境中可用的最佳打印机。\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import init_printing\n",
    "init_printing()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IPython console for SymPy 1.0 (Python 3.6.3-64-bit) (ground types: python)\n",
      "\n",
      "These commands were executed:\n",
      ">>> from __future__ import division\n",
      ">>> from sympy import *\n",
      ">>> x, y, z, t = symbols('x y z t')\n",
      ">>> k, m, n = symbols('k m n', integer=True)\n",
      ">>> f, g, h = symbols('f g h', cls=Function)\n",
      ">>> init_printing()\n",
      "\n",
      "Documentation can be found at http://docs.sympy.org/1.0/\n"
     ]
    }
   ],
   "source": [
    "from sympy import init_session\n",
    "init_session()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAF4AAAAhCAYAAABQphx6AAAABHNCSVQICAgIfAhkiAAABB9JREFU\naIHtmm1oFGcQx38xUWKpbcGoVEWptaIgRiNGEDWnlRZttU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rcZ+iD1dfSm+en2PGoDoE3gf5JRJMM10xEuvr2JAe8MBcpS3FkAmgH/ki6tylN\nvwlpcOkoR0pjM/B6msyazqYY+POB6j5Ddu0G4CzyvW6obGgIvAkMww686SxeBPwJnPHBU5fEk+l1\nPUl3gt5bkWZjuPLedfB1hq4z9WsZkr0tSCP9GRmVG5H+EhYvIL+k9eVf87FuF/CFHyIf+Br//nQ7\nTMYOfJmHbjImAG+E4pHU34sh2c4bKpExaZt+XoAEvYXo/zShwqrpj4BxyMNJO9IgIoSMejo2t5q8\nevSc4BVkhr6P1NI5+XUnQoQIESJE6H74H9YYW4Q6jOjlAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$\\int \\sqrt{\\frac{1}{x}}\\, dx$$"
      ],
      "text/plain": [
       "⌠           \n",
       "⎮     ___   \n",
       "⎮    ╱ 1    \n",
       "⎮   ╱  ─  dx\n",
       "⎮ ╲╱   x    \n",
       "⌡           "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import *\n",
    "x, y, z = symbols('x y z')\n",
    "init_printing()\n",
    "Integral(sqrt(1/x), x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 打印函数\n",
    "\n",
    "#### str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Integral(sqrt(1/x), x)'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import *\n",
    "x, y, z = symbols('x y z')\n",
    "str(Integral(sqrt(1/x), x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### srepr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Integral(Pow(Pow(Symbol('x'), Integer(-1)), Rational(1, 2)), Tuple(Symbol('x')))\""
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srepr(Integral(sqrt(1/x), x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ASCII Pretty Printer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'⌠           \\n⎮     ___   \\n⎮    ╱ 1    \\n⎮   ╱  ─  dx\\n⎮ ╲╱   x    \\n⌡           '"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pretty(Integral(sqrt(1/x), x), use_unicode=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "⌠           \n",
      "⎮     ___   \n",
      "⎮    ╱ 1    \n",
      "⎮   ╱  ─  dx\n",
      "⎮ ╲╱   x    \n",
      "⌡           \n"
     ]
    }
   ],
   "source": [
    "print(pretty(Integral(sqrt(1/x), x), use_unicode=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Unicode Pretty Printer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  /          \n",
      " |           \n",
      " |     ___   \n",
      " |    / 1    \n",
      " |   /  -  dx\n",
      " | \\/   x    \n",
      " |           \n",
      "/            \n"
     ]
    }
   ],
   "source": [
    "pprint(Integral(sqrt(1/x), x), use_unicode=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### LaTeX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\int \\sqrt{\\frac{1}{x}}\\, dx\n"
     ]
    }
   ],
   "source": [
    "print(latex(Integral(sqrt(1/x), x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### MathML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<apply>\n",
      "\t<int/>\n",
      "\t<bvar>\n",
      "\t\t<ci>x</ci>\n",
      "\t</bvar>\n",
      "\t<apply>\n",
      "\t\t<root/>\n",
      "\t\t<apply>\n",
      "\t\t\t<power/>\n",
      "\t\t\t<ci>x</ci>\n",
      "\t\t\t<cn>-1</cn>\n",
      "\t\t</apply>\n",
      "\t</apply>\n",
      "</apply>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sympy.printing.mathml import print_mathml\n",
    "print_mathml(Integral(sqrt(1/x), x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Dot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "digraph{\n",
      "\n",
      "# Graph style\n",
      "\"ordering\"=\"out\"\n",
      "\"rankdir\"=\"TD\"\n",
      "\n",
      "#########\n",
      "# Nodes #\n",
      "#########\n",
      "\n",
      "\"Add(Integer(2), Symbol(x))_()\" [\"color\"=\"black\", \"label\"=\"Add\", \"shape\"=\"ellipse\"];\n",
      "\"Integer(2)_(0,)\" [\"color\"=\"black\", \"label\"=\"2\", \"shape\"=\"ellipse\"];\n",
      "\"Symbol(x)_(1,)\" [\"color\"=\"black\", \"label\"=\"x\", \"shape\"=\"ellipse\"];\n",
      "\n",
      "#########\n",
      "# Edges #\n",
      "#########\n",
      "\n",
      "\"Add(Integer(2), Symbol(x))_()\" -> \"Integer(2)_(0,)\";\n",
      "\"Add(Integer(2), Symbol(x))_()\" -> \"Symbol(x)_(1,)\";\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "from sympy.printing.dot import dotprint\n",
    "from sympy.abc import x\n",
    "print(dotprint(x+2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
